package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.repository.ElevationRepository;
import com.css.fxfzypg.modules.baseData.entity.Elevation;
import com.css.fxfzypg.modules.baseData.service.ElevationService;
import com.css.fxfzypg.modules.baseData.queryParams.ElevationQueryParams;
import com.css.fxfzypg.modules.baseData.vo.ElevationVO;
import com.css.fxfzypg.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Service
public class ElevationServiceImpl implements ElevationService {
    @Autowired
    ElevationRepository elevationRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ElevationVO updateElevation(ElevationVO elevationVO) {

        ElevationVO elevationVO1 = elevationRepository.updateElevation(elevationVO);

        return elevationVO1;
    }

    @Override
    @Transactional
    public Elevation saveElevation(Elevation elevation) {
        elevation.setElevationId(UUIDGenerator.getUUID());
        /*elevation.setElevationStatus(YNEnum.Y.toString());*/
        return elevationRepository.saveElevation(elevation);
    }

    @Override
    public JSONObject getAllElevation(ElevationQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(elevationRepository.getAllElevationParams(queryParams));
    }

    @Override
    public Elevation getElevationById(String id) {
        Elevation elevation = elevationRepository.getElevationById(id);
        return elevation;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteElevations(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        elevationRepository.deleteElevationsById(ids, timestamp);
    }

    @Override
    public List<Elevation> getAll() {

        return elevationRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<Elevation> elevations = FileUtil.importExcel(file, Elevation.class);
        for (int i = 0; i < elevations.size(); i++) {
            elevations.get(i).setElevationId(UUIDGenerator.getUUID());
            /*elevations.get(i).setElevationStatus(YNEnum.Y.toString());*/

        }
        elevationRepository.importExcel(elevations);
    }

    @Override
    public Integer countByCounty(String province, String city, String county) {
        return elevationRepository.countByCounty(province, city, county);
    }

    @Override
    public Integer countByCity(String province, String city) {
        return elevationRepository.countByCity(province, city);
    }

    @Override
    public Integer countByProvince(String province) {
        return elevationRepository.countByProvince(province);
    }

    @Override
    public Integer countByProvinceAndYear(String provinceName, String taskYear) {
        return elevationRepository.countByProvinceAndYear(provinceName, taskYear);
    }
}
